<template>
  <el-dialog
    :title="dialogType ==='dialog_add'? text_add : text_update"
    :close-on-click-modal="isDebug"
    :visible.sync="dialogVisible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm"  label-width="130px" label-position="right">
    <el-form-item label="名称" prop="title">
      <el-input v-model="dataForm.title" placeholder="名称"></el-input>
    </el-form-item>
    <el-form-item label="跳转链接" prop="liveUrl">
      <el-input v-model="dataForm.liveUrl" placeholder="跳转链接"></el-input>
    </el-form-item>
    <el-form-item label="总点赞人数" prop="dianZanCount">
      <el-input v-model="dataForm.dianZanCount" placeholder="总点赞人数"></el-input>
    </el-form-item>
    <el-form-item label="广告按钮文字" prop="adText">
      <el-input v-model="dataForm.adText" placeholder="广告按钮文字"></el-input>
    </el-form-item>
    <el-form-item label="广告按钮跳转的链接" prop="adUrl">
      <el-input v-model="dataForm.adUrl" placeholder="广告按钮跳转的链接"></el-input>
    </el-form-item>
    <el-form-item label="直播室在线总人数" prop="onlineCount">
      <el-input v-model="dataForm.onlineCount" placeholder="直播室在线总人数"></el-input>
    </el-form-item>
    <el-form-item label="直播室喊单成功率" prop="handanRate">
      <el-input v-model="dataForm.handanRate" placeholder="直播室喊单成功率"></el-input>
    </el-form-item>
    <el-form-item label="直播室盈利点" prop="yingLi">
      <el-input v-model="dataForm.yingLi" placeholder="直播室盈利点"></el-input>
    </el-form-item>
    <el-form-item label="直播室主图" prop="mainImg">
      <el-input v-model="dataForm.mainImg" placeholder="直播室主图"></el-input>
    </el-form-item>
    <el-form-item label="直播室字图" prop="subImg">
      <el-input v-model="dataForm.subImg" placeholder="直播室字图"></el-input>
    </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="dataForm.createTime"
          type="datetime"
          value-format="timestamp"
          placeholder="选择时间">
        </el-date-picker>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">{{text_cancel}}</el-button>
      <el-button type="primary" @click="dialogType ==='dialog_add'?requestAdd():requestUpdate()">{{text_confirm}}</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        dialogVisible: false,
        dialogType: 'dialog_add', // 弹窗类型 1添加 2编辑,
        dataForm: {
          id: 0,
          title: '',
          liveUrl: '',
          dianZanCount: '',
          adText: '',
          adUrl: '',
          onlineCount: '',
          handanRate: '',
          yingLi: '',
          mainImg: '',
          subImg: '',
          createTime: '',
          updateTime: ''
        },
        dataRule: {
          title: [{ validator: this.checkText, trigger: 'change' }],
          liveUrl: [{ validator: this.checkText, trigger: 'change' }],
          dianZanCount: [{ validator: this.checkText, trigger: 'change' }],
          adText: [{ validator: this.checkText, trigger: 'change' }],
          adUrl: [{ validator: this.checkText, trigger: 'change' }],
          onlineCount: [{ validator: this.checkText, trigger: 'change' }],
          handanRate: [{ validator: this.checkText, trigger: 'change' }],
          yingLi: [{ validator: this.checkText, trigger: 'change' }],
          mainImg: [{ validator: this.checkText, trigger: 'change' }],
          subImg: [{ validator: this.checkText, trigger: 'change' }],
          createTime: [{ validator: this.checkText, trigger: 'change' }],
          updateTime: [{ validator: this.checkText, trigger: 'change' }]
        }
      }
    },
    props: {
      rowData: {
        type: Object,
        default: null
      }
    },
    methods: {
      initDialog (dialogType) {
        this.dialogVisible = true
        this.dialogType = dialogType
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (dialogType === 'dialog_update') {
            this.requestInfo()
          }
        })
      },
      /** 请求信息(表单回显) */
      requestInfo () {
        this.$httpRequest({
          url: `/api/banner/info/${this.rowData.id}`,
          method: 'get'
        }).then((data) => {
          this.dataForm.title = data.title
          this.dataForm.liveUrl = data.liveUrl
          this.dataForm.dianZanCount = data.dianZanCount
          this.dataForm.adText = data.adText
          this.dataForm.adUrl = data.adUrl
          this.dataForm.onlineCount = data.onlineCount
          this.dataForm.handanRate = data.handanRate
          this.dataForm.yingLi = data.yingLi
          this.dataForm.mainImg = data.mainImg
          this.dataForm.subImg = data.subImg
          this.dataForm.createTime = data.createTime
          this.dataForm.updateTime = data.updateTime
        }, error => {
          this.showErrorToast(error.msg)
        })
      },
       /** 请求添加 */
      requestAdd () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$httpRequest({
              url: `/api/banner/add`,
              method: 'post',
              data: {
                'id': this.dataForm.id || undefined,
                'title': this.dataForm.title,
                'liveUrl': this.dataForm.liveUrl,
                'dianZanCount': this.dataForm.dianZanCount,
                'adText': this.dataForm.adText,
                'adUrl': this.dataForm.adUrl,
                'onlineCount': this.dataForm.onlineCount,
                'handanRate': this.dataForm.handanRate,
                'yingLi': this.dataForm.yingLi,
                'mainImg': this.dataForm.mainImg,
                'subImg': this.dataForm.subImg,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime
              }
            }).then(() => {
              this.dialogVisible = false
              this.showSuccessToast(this.toast_text_add_success, () => {
                this.$emit('refreshDataList')
              })
            }, error => {
              this.showErrorToast(error.msg)
            })
          }
        })
      },
      /** 请求更新 */
      requestUpdate () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$httpRequest({
              url: `/api/banner/add`,
              method: 'post',
              data: {
                'id': this.dataForm.id || undefined,
                'title': this.dataForm.title,
                'liveUrl': this.dataForm.liveUrl,
                'dianZanCount': this.dataForm.dianZanCount,
                'adText': this.dataForm.adText,
                'adUrl': this.dataForm.adUrl,
                'onlineCount': this.dataForm.onlineCount,
                'handanRate': this.dataForm.handanRate,
                'yingLi': this.dataForm.yingLi,
                'mainImg': this.dataForm.mainImg,
                'subImg': this.dataForm.subImg,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime
              }
            }).then(() => {
              this.dialogVisible = false
              this.showSuccessToast(this.toast_text_add_success, () => {
                this.$emit('refreshDataList')
              })
            }, error => {
              this.showErrorToast(error.msg)
            })
          }
        })
      }
    }
  }
</script>
